Immersive Sprint - web historian

checkpoint 12

module.exports === exports 가 가리키는 건 같음.
하나의 object가 묶여져있음.

1.
var x = 10
var mod = require('./lib/my-module.js') // {X :30}이 담겨있음
var result = mod.x

3. 동일한 모듈을 두 번 부르는 경우,

같은 모듈은 한번만 불러옴.
mod1과 mod2는 같은 메모리를 참조한다
3번과 4번은 같은 맥락이다.

5번도 같은 맥락으로 1번만 불러온다.
require만 해도 글로벌 scope에 있는 애들은 전부
실행이 된다!(exports하지 않아도)
node에서도 experimenatal 주면 import export 실행해볼 수 있음

8. 스택이 다 비워지고 나서야 비동기로 가기 때문에
ACB
console.log("A");
setTimeout(function() {
  console.log("B");
}, 1000);

superLongComputation();

console.log("C");

9

1~5 공식 모듈의 작동방식 () https://nodejs.org/dist/latest-v8.x/docs/api/modules.html 꼭 한번 읽어봐야 함. 두 번 불리면 한 번 불리고, 상호 참조에 대해서도 찾아볼 칠요가 있다.

http://nodejs.sideeffect.kr/docs/v0.8.20/api/modules.html 읽기!!

promise는 코딩 기법

async await은 promise를 기반으로 만들어짐

async function() {
    var existingUser = await finduserInDataBase(user);
    var securedUser = await hashPassword(user);

}

이미지 파일은 고려하지 않고 html만 저장하기

날 것을 받아오는 것

리다이렉트 되는 사이트들은 제대로 작동하지 않음

html만 저장하면 됨, 리소스는 저장하지 않아도 됨

https도 고려해야 함

한 파일에 다 넣지 말고, 분리해서 구현할 것

worker가 있으면 각각의 역할을 분리해서

하나는 html만 분리하는 식으로

cron은 다 하고 해도 됨

풀어보다가 test가 무의미하다가 작동되게만 해도 상관없음

서버를 껐다 켜도 데이터를 보존하기 위해서 파일을 씀

routing 주소를 분기시켜주는 개념

request.handler에서는 분기시키는 내용을 넣으면 됨

(지난 레퍼런스 코드 참조하면 될듯)

NODE.js 콜백의 기본패턴

콜백의 개념을 이해하기가 말처럼 쉽지 않았다. 아래 링크들이 콜백을 이해하는데 큰 도움을 주었다.

에러 우선 콜백을 이해하기

fs.readFile('/foo.txt', function(err, data) {
  // If an error occurred, handle it (throw, propagate, etc)
  if (err) {
    console.log('Unknown Error')
    return
  }
  // Otherwise, log the file contents
  console.log(data)
})
unction strictAddition(x, y, callback) {
if(typeof x !== ‘number’) {
callback( new Error(‘First argument is not a number’) );
return;
}
if(typeof y !== ‘number’) {
callback( new Error(‘Second argument is not a number’) );
return;
}
var result = x + y;
setTimeout(function() {
callback(null, result);
}, 500);
}
// The Callback
function callback(err, data) {
if(err) {
console.log(err);
return;
}
console.log(data);
}
// Examples
strictAddition(2, 10, callback); // 12
strictAddition(-2, 10, callback); // 8
strictAddition(‘uh oh’, 10, callback); // Error = “First argument is not a number”
strictAddition(2, ‘10’, callback); // // Error = “Second argument is not a number”
// Async Example - all calls made in parallel
async.parallel({
twelve: function(callback){ strictAddition(2, 10, callback); },
fiftythree: function(callback){ strictAddition(42, 11, callback); },
six: function(callback){ strictAddition(23, -17, callback); },
},
function(err, results) {
if(err) {
console.log(err);
return;
}
console.log(results); // {twelve: 12, fiftythree: 53, six: 6}
});

콜백과 프로미스보다 훨씬 쉬워보이는 async지만 웹 히스토리안에선 아직 다루지 않는다.

/ Example taken from caolan/async README
async.parallel({
    one: function(callback){
        setTimeout(function(){
            callback(null, 1);
        }, 200);
    },
    two: function(callback){
        setTimeout(function(){
            callback(null, 2);
        }, 100);
    }
},
function(err, results) {
    // results is equal to: {one: 1, two: 2}
});

lastrites2018
Written bylastrites2018
I explain with words and code.

👉삽질의 역사는 노션 WIKI에서